package mapEditor.view.resItemEdit
{
	import asset.mapEditorResItemItemAsset;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import iso.IsoGrid;
	
	import mapEditor.MapEditorManager;
	import mapEditor.data.ConfigTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.ResItemItemVO;
	import mapEditor.vo.ResItemVO;
	
	import utils.ApplicationDomainUtil;
	import utils.DisplayUtil;
	
	public class ResItemItemView extends mapEditorResItemItemAsset
	{
		/**
		 *物件类型：1=单项物件;2=组合物件
		 */
		private var _type:int;
		/**
		 *是否选中 
		 */		
		private var _isSelected:Boolean;
		/**
		 *是否编辑中 
		 */		
		private var _isEdit:Boolean;
		private var _resItemVO:ResItemVO;
		private var _isoGrid:IsoGrid;
		private var _items:Vector.<ResItemEditItemView>=new Vector.<ResItemEditItemView>();
		
		public function ResItemItemView(resItemVO:ResItemVO)
		{
			_resItemVO=resItemVO;
			_type=_resItemVO.type;
			gotoAndStop(1);
			
			doubleClickEnabled=true;
			mouseChildren=false;
			addEventListener(MouseEvent.MOUSE_OVER, __onMouseEvent);
			addEventListener(MouseEvent.MOUSE_OUT, __onMouseEvent);
			addEventListener(MouseEvent.DOUBLE_CLICK, __onMouseEvent);
			addEventListener(MouseEvent.CLICK, __onMouseEvent);
			_resItemVO.addEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
			
			setInfo();
		}
		
		private function __onPropertyChange(evt:MapEditorEvent):void
		{
			setInfo();
		}
		
		private function setInfo():void
		{
			_isoGrid=new IsoGrid(2, ConfigTemplateInfo.instance.mapVO.tileSize, _resItemVO.gridRow, _resItemVO.gridCols, true);
			posShow.addChild(_isoGrid);
			txtName.text=_resItemVO.name;
			txtID.text=_resItemVO.id.toString();
			for each(var resItemItemVO:ResItemItemVO in _resItemVO.items)
			{
				var resItemEditItemView:ResItemEditItemView=new ResItemEditItemView(resItemItemVO);
				resItemEditItemView.resItemItemVO.addEventListener(Event.CHANGE, __onResItemEditItemChange);
				resItemEditItemView.mouseEnabled=false;
				_isoGrid.addChild(resItemEditItemView);
				_items.push(resItemEditItemView);
			}
			resetSize();
		}
		
		private function resetSize():void
		{
			var scale:Number=1;
			if(_isoGrid.width>60)
			{
				scale=60/_isoGrid.width;
			}
			if(_isoGrid.height>60)
			{
				scale=60/_isoGrid.height<=scale ? 60/_isoGrid.height : scale;
			}
			_isoGrid.scaleX=_isoGrid.scaleY=scale;
			_isoGrid.x=(60-_isoGrid.width)/2+_isoGrid.width/4;
			_isoGrid.y=0;
		}
		
		private function __onResItemEditItemChange(evt:Event):void
		{
//			resetSize();
		}
		
		private function clearItems():void
		{
			while(_items && _items.length>0)
			{
				var resItemEditItemView:ResItemEditItemView=_items.shift();
				resItemEditItemView.resItemItemVO.removeEventListener(Event.CHANGE, __onResItemEditItemChange);
				DisplayUtil.dispose(resItemEditItemView);
				resItemEditItemView=null;
			}
		}
		
		private function __onMouseEvent(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.MOUSE_OVER:
					if(!_isSelected)gotoAndStop(2);
					break;
				case MouseEvent.MOUSE_OUT:
					if(!_isSelected)gotoAndStop(1);
					break;
				case MouseEvent.DOUBLE_CLICK:
					isEdit=true;
					break;
				case MouseEvent.CLICK:
					isSelected=true;
					break;
			}
		}
		
		/**
		 *是否选中
		 */
		public function get isSelected():Boolean
		{
			return _isSelected;
		}
		
		/**
		 * @private
		 */
		public function set isSelected(value:Boolean):void
		{
			if(_isSelected == value)return;
			_isSelected = value;
			gotoAndStop(_isSelected ? 2 : 1);
			if(_isSelected)dispatchEvent(new MapEditorEvent(MapEditorEvent.SELECTED));
		}
		
		/**
		 *是否编辑中
		 */
		public function get isEdit():Boolean
		{
			return _isEdit;
		}
		
		/**
		 * @private
		 */
		public function set isEdit(value:Boolean):void
		{
			if(_isEdit == value)return;
			_isEdit = value;
			if(_isEdit)dispatchEvent(new MapEditorEvent(MapEditorEvent.EDITING));
		}

		public function get resItemVO():ResItemVO
		{
			return _resItemVO;
		}

		public function set resItemVO(value:ResItemVO):void
		{
			_resItemVO = value;
		}
		
		public function dispose():void
		{
			removeEventListener(MouseEvent.MOUSE_OVER, __onMouseEvent);
			removeEventListener(MouseEvent.MOUSE_OUT, __onMouseEvent);
			removeEventListener(MouseEvent.DOUBLE_CLICK, __onMouseEvent);
			removeEventListener(MouseEvent.CLICK, __onMouseEvent);
			_resItemVO.removeEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
			
			clearItems();
			_items=null;
			_resItemVO=null;
			DisplayUtil.dispose(_isoGrid);
			_isoGrid=null;
			if(parent)parent.removeChild(this);
		}
	}
}